package com.qiguliuxing.dts.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import com.qiguliuxing.dts.db.dao.DtsBrandMapper;
import com.qiguliuxing.dts.db.dao.DtsCategoryMapper;
import com.qiguliuxing.dts.db.domain.DtsBrand;
import com.qiguliuxing.dts.db.domain.DtsCategory;

@Service
public class DtsBrandService {
	@Resource
	private DtsCategoryMapper categoryMapper;
	
	@Resource
	private DtsBrandMapper brandMapper;

	public List<DtsBrand> queryVO(int offset, int limit) {
		QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("deleted", false);
		queryWrapper.orderByDesc("add_time");
		IPage<DtsBrand> iPage = new Page<>(offset, limit);
		iPage = brandMapper.selectPage(iPage, queryWrapper);
		return iPage.getRecords();
	}

	public int queryTotalCount() {
		QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("deleted", false);
		return brandMapper.selectCount(queryWrapper).intValue();
	}

	public DtsBrand findById(Integer id) {
		return brandMapper.selectById(id);
	}

	public List<DtsBrand> querySelective(String id, String name, Integer page, Integer size, String sort,
			String order) {
		QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(id)) {
			queryWrapper.eq("id", id);
		}
		if (StrUtil.isNotEmpty(name)) {
			queryWrapper.like("name", id);
		}
		if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)) {
			queryWrapper.orderByDesc(sort);
		}
		queryWrapper.eq("deleted", false);
		IPage<DtsBrand> iPage = new Page<>(page, size);
		iPage = brandMapper.selectPage(iPage, queryWrapper);
		return iPage.getRecords();
	}

	public int updateById(DtsBrand brand) {
		brand.setUpdateTime(LocalDateTime.now());
		return brandMapper.updateById(brand);
	}

	public void deleteById(Integer id) {
		brandMapper.deleteById(id);
	}

	public void add(DtsBrand brand) {
		brand.setAddTime(LocalDateTime.now());
		brand.setUpdateTime(LocalDateTime.now());
		brandMapper.insert(brand);
	}

	public List<DtsBrand> all() {
		QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("deleted", false);
		return brandMapper.selectList(queryWrapper);
	}
	
	public List<DtsBrand> getAdminBrands(Integer adminId) {
		if (adminId == null) {
			return null;
		}
		QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("deleted", false);
		queryWrapper.lambda().eq(DtsBrand::getAdminId, adminId);
		return brandMapper.selectList(queryWrapper);
	}

	/**
	 * 根据分类id获取分类名
	 * @param categoryId
	 * @return
	 */
	public String getBrandCategory(Integer categoryId) {
		DtsCategory dtsCategory = categoryMapper.selectById(categoryId);
		return dtsCategory == null ? "综合" : dtsCategory.getName();
	}
	
}
